<template>
  <div>
    <div>
      <el-dialog title="添加角色" :visible.sync="$props.visible" width="50%" @close="handleClose">

        <div class="add-content">
          <el-form
            ref="formName"
            :model="staffFrom"
            :rules="staffRules"
            label-width="120px"
            style="margin-top: 10px;"
          >
            <el-form-item label="角色名称：" prop="name">
              <el-input v-model="staffFrom.name" placeholder="请输入角色名称" style="width: 60%;" />
            </el-form-item>
            <el-form-item label="角色类型：" prop="accessLevel">
              <el-select v-model="staffFrom.accessLevel" filterable placeholder="请选择" style="width: 60%;">
                <el-option
                  v-for="item in validRoleTypesList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="角色描述：" prop="descpt">
              <el-input v-model="staffFrom.descpt" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="请输入角色描述" style="width: 60%;" />
            </el-form-item>
          </el-form>
        </div>

        <span class="dialog-footer">
          <div style="margin-top:15px;">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" @click="handleSubmit">提交</el-button>
          </div>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { roleAdd } from '@/api/iam/iamRole'
import * as R from 'ramda'
import { isSystemAdmin } from '@/utils/permission'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    roleTypesList: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      staffFrom: {
        name: '',
        accessLevel: '',
        descpt: ''
      },
      staffRules: {
        name: [{ required: true, message: '请填写角色名称', trigger: 'blur' }],
        accessLevel: [{ required: true, message: '请选择角色类型', trigger: 'blur' }],
        descpt: [{ required: false, message: '请填写角色描述', trigger: 'blur' }]
      },
      validRoleTypesList: R.filter(x => x.canCreateFromHere, this.$props.roleTypesList),
      isSystemAdmin: isSystemAdmin()
    }
  },
  created() {

  },
  methods: {
    // 弹出框关闭后触发
    handleClose() {
      // 子组件调用父组件方法，并传递参数
      this.$emit('handleClose')
    },
    handleSuccess() {
      this.$message({
        message: '添加成功',
        type: 'success'
      })
      this.handleClose()
      this.$emit('handleSuccess')
    },
    // 新增
    handleSubmit() {
      this.$refs['formName'].validate((valid) => {
        if (valid) {
          roleAdd(this.staffFrom).then((response) => {
            this.handleSuccess()
          })
        }
      }
      )
    }
  }
}
</script>
<style>
.add-content {
    border: 1px solid #e4e4e4;
    width: 90%;
    margin: 0 auto;
    height: 250px;
}
.dialog-footer{
    width: 97%;
    display: flex;
    justify-content:center;
}
</style>
